//
//  DNDairyManager.m
//  DanoneEA
//
//  Created by Senela on 16/10/18.
//  Copyright © 2016年 翔傲科技. All rights reserved.
//

#import "DNDairyManager.h"


@implementation DNDairyManager


#pragma mark - Public methods

+ (instancetype)sharedInstance {
    static DNDairyManager *sharedInstance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
         sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

-(NSDate *)getLastFourDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
    
    NSDateComponents *component = [calendar components:unit fromDate:[NSDate date]]; 
    component.day-=3;
    component.hour = 0;
    component.minute = 0;
    component.second = 0;
    
    NSDate *dateFromDateComponentsForDate = [calendar dateFromComponents:component];
    
    return dateFromDateComponentsForDate;
}

-(BOOL)addDiary:(DNDairyBaseModel *)diaryInfo
{
    BOOL ret = NO;
    diaryInfo.status = [NSNumber numberWithInt:OPStatus_ADD];
         
    ret = [self saveDairyToLocalDB:diaryInfo];
    return ret;
}

-(BOOL)deleteDiary:(DNDairyBaseModel *)diaryInfo
{
    BOOL ret = NO;
    
    if(diaryInfo.serverId == nil)
    {
        // delete diaty info from local database
        ret = [self deleteDairyFromLocalDBByAppId:diaryInfo.appId];
    }
    else
    {
        diaryInfo.status = [NSNumber numberWithInt:OPStatus_DELETE];
        diaryInfo.deleteTime =[NSString stringWithFormat:@"%.0f",  [[NSDate date] timeIntervalSince1970]];
        ret = [self saveDairyToLocalDB:diaryInfo];
    }
    return ret;
}

-(BOOL)modifyDiary:(DNDairyBaseModel *)diaryInfo
{
    BOOL ret = NO;
    diaryInfo.modifyTime = [NSString stringWithFormat:@"%.0f",  [[NSDate date] timeIntervalSince1970]];
    
    if(diaryInfo.serverId == nil)
    {
        // Modify the offline data, appId already exists［don‘t update］, only to modify the createTime
        diaryInfo.status = [NSNumber numberWithInt:OPStatus_ADD];
        ret = [self saveDairyToLocalDB:diaryInfo]; 

        return ret;
    }
    else
    {
        diaryInfo.status = [NSNumber numberWithInt:OPStatus_MODIFY];
        ret = [self saveDairyToLocalDB:diaryInfo];
    }
    return ret;
}

-(BOOL)saveDairyToLocalDB:(DNDairyBaseModel *)diaryInfo
{
    BOOL ret = NO;
    return ret;
}

-(BOOL)deleteDairyFromLocalDBByAppId:(NSString *)appId
{
    BOOL ret = NO;
    return ret;
}

#pragma mark - Private methods
-(instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}


@end
